package swing;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;

import lib.LogTableModel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.text.SimpleDateFormat;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LogTable extends JPanel {

	private JTable table;
	private LogTableModel tableModel;
	private static JTabbedPane tabPane=null;
	/**
	 * Create the panel.
	 */
	public LogTable(JTabbedPane tabbedPane) {
		setBorder(new LineBorder(new Color(0, 0, 0)));
		this.tabPane=tabbedPane;
		setLayout(null);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		double width = screenSize.getWidth();
		double height = screenSize.getHeight();
		tableModel = new LogTableModel();
	
		table = new JTable(tableModel);
		table.setPreferredScrollableViewportSize(new Dimension((int) (width-200),(int)height-200));
		table.setFillsViewportHeight(true);

		TableColumnModel tcm = table.getColumnModel();
		tcm.getColumn(0).setMaxWidth(40);
		tcm.getColumn(1).setMaxWidth(120);
		tcm.getColumn(0).setPreferredWidth(40);
		tcm.getColumn(1).setPreferredWidth(120);
		tcm.getColumn(1).setCellRenderer(new DateCellRenderer());
		JScrollPane scroll= new JScrollPane(table);
		scroll.setBounds(6, 5, (int) (width-140), (int)height-100);
		add(scroll);
		
		JButton btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int[] rows = table.getSelectedRows();
				//while(rows.length>0)
				//{
				//	tableModel.removeRow(rows[0]);
				//}
				tableModel.removeRows(rows);
				
			}
		});
		btnDelete.setBounds(1252, 21, 89, 34);
		add(btnDelete);
		
		JButton btnDeleteall = new JButton("DeleteAll");
		btnDeleteall.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tableModel.removeAll();
			}
		});
		btnDeleteall.setBounds(1252, 83, 89, 34);
		add(btnDeleteall);
		
		JButton btnApdate = new JButton("Update");
		btnApdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
			}
		});
		btnApdate.setToolTipText("Cap nhat lai du lieu cua bang tu database");
		btnApdate.setBounds(1252, 150, 89, 34);
		add(btnApdate);
	}
	
	
	public class DateCellRenderer extends DefaultTableCellRenderer {
	    public DateCellRenderer() { super(); }

	    @Override
	    public void setValue(Object value) {
	        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy hh:mm:ss");

	        setText((value == null) ? "" : sdf.format(value));
	    }
	}
}
